package com.zy.utils;

import java.sql.*;

/**
 * @Author GongXl
 * @Description 数据库工具
 * @Date 022 10:50
 * @Param
 * @return
**/
public class DbUtil {

    public static final String URL = "jdbc:mysql://127.0.0.1:3306/mall";
    public static final String USER = "root";
    public static final String PASSWORD = "root";
    /**
     * @Author GongXl
     * @Description 获得连接对象
     * @Date 022
     * @Param []
     * @return java.sql.Connection
    **/
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        //1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获得数据库连接
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        return conn;
    }

    public static void main(String[] args) throws Exception {
        //1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获得数据库连接
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        //3.操作数据库，实现增删改查
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM m_users");
        //如果有数据，rs.next()返回true
        while(rs.next()){
            System.out.println(rs.getString("userName")+" pas："+rs.getString("password"));
        }
    }
}
